# -*- coding: cp936 -*-



import sys, pygame, random,math
from pygame.locals import *
pygame.init()
width, height = 480,480
screen = pygame.display.set_mode((width,height))
black, red, blue, white = (0, 0, 0), (255, 0, 0), (0, 0, 255), (255, 255, 255)
list1=[[0,0,0] ,[0,0,0],[0,0,0]]
number=1


def draw_game():
    pygame.draw.rect(screen,white,[0,0,480,480],0)#画出一个矩形

    pygame.draw.line(screen,black,(160,0),(160,480),5)#pygame.draw.line(screen,[颜色],(坐标y,x),(坐标y,x),宽)

    pygame.draw.line(screen,black,(320,0),(320,480),5)
    pygame.draw.line(screen,black,(0,160),(480,160),5)
    pygame.draw.line(screen,black,(0,320),(480,320),5)
    for row, line in enumerate(list1):
        for col, val in enumerate(line):
            if val == 1:
                # Draw an X as two crossing lines.

                upper_left = (col * 160 + 5, row * 160 + 5)
                lower_right = (col * 160 + 155, row * 160 + 155)
                pygame.draw.line(screen, red, upper_left, lower_right, 5)

                upper_right = (col * 160 + 155, row * 160 + 5)
                lower_left = (col * 160 + 5, row * 160 + 155)
                pygame.draw.line(screen, red, upper_right, lower_left, 5)
            elif val == -1:
                rect = (col * 160 + 5, row * 160 + 5, 150, 150)
                pygame.draw.ellipse(screen, blue, rect, 5)
            else:
                assert val == 0
                continue
    pygame.display.flip()#刷新屏幕
draw_game()



while True:
    
    
    x=None
    event = pygame.event.wait()
    if event.type == pygame.QUIT:
        
        pygame.quit()
        exit(0)
    elif event.type == MOUSEBUTTONDOWN and event.button==1:
        
        x = pygame.mouse.get_pos()#检查鼠标位置
        
        x1=x[0]/160 #判断在二维列表的位置

        x2=x[1]/160
        
    if x==None:
        continue
    if list1[x2][x1] ==0:
        list1[x2][x1]=number
        number=number*-1
        draw_game()

